package com.example.drools.spring.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.example.drools.spring.core.impl.ProductRuleEngineImpl;
import com.example.drools.spring.dao.RuleRepository;
import com.example.drools.spring.entity.CheckResult;
import com.example.drools.spring.entity.DroolModel;
import com.example.drools.spring.entity.DroolRule;
import com.example.drools.spring.entity.Product;
import com.example.drools.spring.entity.db.Rule;
import com.example.drools.spring.utils.JacksonUtil;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ddsyw
 * @version 1.0
 * @date 2021/12/28 5:24 下午
 */
@RequestMapping("/test")
@Controller
@Slf4j
public class TestController {

  @Resource
  private KieSession kieSession;

  @Resource
  private ProductRuleEngineImpl productRuleEngine;

  @Resource
  private RuleRepository ruleRepository;

  @ResponseBody
  @RequestMapping("/product")
  public String test(@RequestBody Map<String,String> map){
    CheckResult checkResult = new CheckResult();
    productRuleEngine.executeRuleEngine(map, checkResult);
    if(checkResult.isOk()){
      System.out.println("规则校验通过");
      return "success";
    }
    return "fail";
  }


  @ResponseBody
  @RequestMapping("/checkNull")
  public String test(@RequestBody List<Map<String,String>> maps){
    if (CollectionUtil.isEmpty(maps)) {
      return "param is null";
    }
    int count = 0;
    for (Map<String,String> map : maps) {
      CheckResult checkResult = new CheckResult();
      productRuleEngine.executeRuleEngine(map, checkResult);
      if(checkResult.isOk()){
        count ++;
      }
    }
    return "success:" + count + "; fail:" + (maps.size() - count);
  }


  @ResponseBody
  @RequestMapping("/refresh")
  public String refresh(@RequestBody DroolRule droolRule){
    DroolModel droolModel = new DroolModel();
    droolRule.setRule("checkValue");
    droolModel.setDroolRules(Arrays.asList(droolRule));
    Rule ruleD = new Rule();
    ruleD.setId(1L);
    ruleD.setRule(JacksonUtil.toJsonString(droolModel));
    ruleRepository.saveAndFlush(ruleD);
    productRuleEngine.refreshEnginRule();
    return "success";
  }

  @ResponseBody
  @RequestMapping("/productV2")
  public String testV2(){
    Product product = new Product();
    product.setSize(1111);
    Product product1 = new Product();
    product1.setSize(1);
    Product product2 = new Product();
    product2.setSize(200);
    kieSession.insert(product);
    kieSession.insert(product1);
    kieSession.insert(product2);
    kieSession.fireAllRules();
    /**
     * fixme  通过校验获取查询语句
     */
    QueryResults queryResults = kieSession.getQueryResults("query-1");
    for (QueryResultsRow queryResultsRow : queryResults) {
      Product person = (Product) queryResultsRow.get("$product");
      log.info("Person from WM, age : " + person.getSize());
    }
    return "success";
  }
}
